/*
 * Giffel, Android application
 *
 * Copyright (C) 2011  Mid Sweden University
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package se.appening;

import static se.appening.util.BitmapImageUtil.loadFrom;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class MainWidget extends AppWidgetProvider {

    static int x = 0;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        if (MainActivity.controller == null) {
            // MainActivity.controller = new MainController(context, new MockCringleGetter());
            MainActivity.controller = new MainController(context);
        }

        Thread t = new UpdateThread(context);
        t.start();
    }

    public static class UpdateThread extends Thread {

        Context context;

        public UpdateThread(Context context) {
            this.context = context;
        }

        @Override
        public void run() {
            while (true) {
                RemoteViews updateViews = null;
                updateViews = new RemoteViews(context.getPackageName(), R.layout.widget);

                Entry[] entries = MainActivity.controller.getEntries();

                Intent intentForSectionOne = new Intent(context, InfoActivity.class);
                intentForSectionOne.putExtra("entry", 0);
                PendingIntent pendingIntentForSectionOne = PendingIntent.getActivity(
                        context, 0, intentForSectionOne, PendingIntent.FLAG_UPDATE_CURRENT);
                updateViews.setOnClickPendingIntent(R.id.linearLayout1, pendingIntentForSectionOne);

                Intent intentForSectionTwo = new Intent(context, InfoActivity.class);
                intentForSectionTwo.putExtra("entry", 1);
                PendingIntent pendingIntentForSectionTwo = PendingIntent.getActivity(
                        context, 1, intentForSectionTwo, PendingIntent.FLAG_UPDATE_CURRENT);
                updateViews.setOnClickPendingIntent(R.id.linearLayout2, pendingIntentForSectionTwo);

                Intent intentForMoreButton = new Intent(context, MainActivity.class);
                PendingIntent pendingIntentForMoreButton = PendingIntent.getActivity(
                        context, 0, intentForMoreButton, 0);
                updateViews.setOnClickPendingIntent(R.id.morebutton, pendingIntentForMoreButton);

                if (entries.length == 0) {
                    updateViews.setTextViewText(R.id.title12, " ");
                    updateViews.setTextViewText(R.id.type1, "No Nearby Locations");
                    updateViews.setTextViewText(R.id.distance1, " ");
                    updateViews.setImageViewResource(R.id.thumb1, R.drawable.empty);

                    updateViews.setTextViewText(R.id.title2, "");
                    updateViews.setTextViewText(R.id.type2, " ");
                    updateViews.setTextViewText(R.id.distance2, " ");
                    updateViews.setImageViewResource(R.id.thumb2, R.drawable.empty);
                } else if (entries.length == 1) {
                    updateViews.setTextViewText(R.id.title12, entries[0].getTitle());
                    updateViews.setTextViewText(R.id.type1, entries[0].getType());
                    updateViews.setTextViewText(R.id.distance1,
                            String.valueOf(entries[0].getDistance()));
                    if (entries[0].getImageUrl().equalsIgnoreCase("")) {
                        updateViews.setImageViewResource(R.id.thumb1, R.drawable.sevardthumb);
                    } else {
                        updateViews.setImageViewBitmap(R.id.thumb1,
                                loadFrom(entries[0].getImageUrl(), context, R.drawable.empty));
                    }

                    updateViews.setTextViewText(R.id.title2, " ");
                    updateViews.setTextViewText(R.id.type2, " ");
                    updateViews.setTextViewText(R.id.distance2, " ");
                    updateViews.setImageViewResource(R.id.thumb2,
                            R.drawable.empty);

                } else { // For more than two
                    updateViews.setTextViewText(R.id.title12, entries[0].getTitle());
                    updateViews.setTextViewText(R.id.type1, entries[0].getType());
                    updateViews.setTextViewText(R.id.distance1,
                            String.valueOf(entries[0].getDistance()));
                    if (entries[0].getImageUrl().equalsIgnoreCase("")) {
                        updateViews.setImageViewResource(R.id.thumb1, R.drawable.sevardthumb);
                    } else {
                        updateViews.setImageViewBitmap(R.id.thumb1,
                            loadFrom(entries[0].getImageUrl(), context, R.drawable.empty));
                    }

                    updateViews.setTextViewText(R.id.title2, entries[1].getTitle());
                    updateViews.setTextViewText(R.id.type2, entries[1].getType());
                    updateViews.setTextViewText(R.id.distance2,
                            String.valueOf(entries[1].getDistance()));
                    updateViews.setImageViewResource(R.id.thumb2, R.drawable.sevardthumb);
                    if (entries[1].getImageUrl().equalsIgnoreCase("")) {
                        updateViews.setImageViewResource(R.id.thumb2, R.drawable.sevardthumb);
                    } else {
                        updateViews.setImageViewBitmap(R.id.thumb2,
                                loadFrom(entries[1].getImageUrl(), context, R.drawable.empty));
                    }

                }

                ComponentName thisWidget = new ComponentName(context, MainWidget.class);
                AppWidgetManager manager = AppWidgetManager.getInstance(context);
                manager.updateAppWidget(thisWidget, updateViews);

                try {
                    Thread.sleep(10000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    }

}
